<template>
  <div class="user-box">
    <el-button size="mini" @click="dialogVisible = true">新增</el-button>
    <!-- 查找 -->
    <el-form :inline="true" :model="userData" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="userData.name" placeholder="请输入用户姓名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    
    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <el-form
        :rules="rules"
        :inline="true"
        ref="form"
        :model="form"
        label-width="80px"
      >
        <!-- 姓名 -->
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <!-- 年龄 -->
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age"></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" :value="0"></el-option>
            <el-option label="女" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <!-- 出生日期 -->
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.birth"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>

        <!-- 地址 -->
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>

      <el-table-column prop="sex" label="性别" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            scope.row.sex === 1 ? "女" : "男"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="birth" label="出生日期" width="180">
      </el-table-column>

      <el-table-column prop="addr" label="地址" width="180"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        @current-change="changePage"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
</style>

<script>
import { getUser, editUser, addUser, delUser } from "../api/index.js";

export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        birth: [{ required: true, message: "请输入出生日期", trigger: "blur" }],
        addr: [{ required: true, message: "请输入地址", trigger: "blur" }],
      },
      tableData: [],
      modelType: 0,
      // 自定义的页码数
      pageData: {
        page: 1,
        limit: 10,
      },
      // 总条数
      total: 0,
      // 查找的用户
      userData:{
        name:''
      }
    };
  },
  methods: {
    // 用户提交表单
    submit() {
      this.$refs.form.validate((val) => {
        if (val) {
          // console.log(this.form);
          if (this.modelType === 0) {
            addUser(this.form).then(() => {
              // 重新获取数据
              this.getList();
            });
          } else {
            editUser(this.form).then(() => {
              // 重新获取数据
              this.getList();
            });
          }

          // 对该表单项进行重置，将其值重置为初始值并移除校验结果
          this.$refs.form.resetFields();
          this.dialogVisible = false;
        }
      });
    },
    // 关闭弹窗
    handleClose() {
      // 对该表单项进行重置，将其值重置为初始值并移除校验结果
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.modelType = 1;
      this.dialogVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 分页器功能
    changePage(val) {
      // console.log(val);
      this.pageData.page = val;
      this.getList();
    },
    // 查询
    onSubmit(){
      this.getList()
    },
    // 获取列表数据
    getList() {
      getUser({ params: {...this.userData,...this.pageData}}).then(({ data }) => {
        console.log(data);
        this.tableData = data.list;
        this.total = data.count || 0;
      });
    },
  },
  mounted() {
    this.getList();
  },
};
</script>